# PoP - Policing Socio-Geographic Boundaries and Inequality
# script for creating figures and tables in Appendix F
# Appendix F Table 16: Influence of Racial Boundary on Police Stops
# Appendix F Figure 9: Influence of Racial Boundary on Police Stops (Standardized).
# Appendix F Table 17: Influence of Logged Crime on Logged Stops (Standardized), Conditional on Racial Boundary Status.
# Appendix F Figure 10: Influence of Percent White on Police Stops, Conditional on Racial Boundary Status.
# Appendix F Table 18: Influence of Percent White on Logged Stops (Standardized), Conditional on Racial Boundary Status
# Appendix F Table 19: Table 19: Influence of Percent White on Police Stops by Race of Civilian, Conditional on Racial Boundary Status
# Appendix F Figure 11: Influence of Percent Poverty on Police Stops by Race of Civilian, Conditional on Racial Boundary Status.
# Appendix F Table 20:  Influence of Percent Poverty on Police Stops by Race of Civilian, Conditional on Racial Boundary Status

suppressPackageStartupMessages(
  
  {
    library(AER)
    library(dplyr)
    library(fixest)
    library(lfe)
    library(tidyverse)
    library(ggplot2)
    library(MASS)
    library(sensemakr)
    library(haven)
    library(readstata13)
    library(readxl)
    library(readr)
    library(gridExtra)
    library(areal)
    library(car)
    library(estimatr)
    library(magrittr)
    library(texreg)
    library(sandwich)
    library(jtools)
    library(ggthemes)
    library(meta)
  }
)

## coefficient plots - all stops ##

# load stop data
load("aus_stops_final.RData")
# now run analyses

# log and +1 to variables (pop already logged)
aus_stops_final = aus_stops_final %>% 
  mutate(lmhhi = log(mhhi + 1),
         larrests = log(total_arrests + 1),
         lmisdemeanors = log(misdemeanor_arrests + 1),
         lfelonies = log(felony_arrests + 1),
         lnonviolent = log(nonviolent_arrests + 1),
         lviolent = log(violent_arrests + 1),
         lsociety = log(society_arrests + 1),
         lperson = log(person_arrests + 1),
         lproperty = log(property_arrests + 1),
         lcrime = log(crime_all + 1),
         lpropertycrime = log(crime_property + 1),
         lviolentcrime = log(crime_violent + 1))

# now for new stop vars
aus_stops_final = aus_stops_final %>%
  mutate(lall_stops_total = log(all_stops_total + 1),
         lall_stops_black = log(all_stops_black + 1),
         lall_stops_latino = log(all_stops_latino + 1),
         lall_stops_white = log(all_stops_white + 1),
         lall_stops_asian = log(all_stops_asian + 1),
         lall_stops_nonwhite = log(all_stops_nonwhite + 1),
         lped_stops_total = log(ped_stops_total + 1),
         lped_stops_black = log(ped_stops_black + 1),
         lped_stops_latino = log(ped_stops_latino + 1),
         lped_stops_white = log(ped_stops_white + 1),
         lped_stops_asian = log(ped_stops_asian + 1),
         lped_stops_nonwhite = log(ped_stops_nonwhite + 1))

# create scaled DVs (to account for different pop. sizes and density in blocks)
aus_stops_final$larrest_sd <- aus_stops_final$larrests - (mean(aus_stops_final$larrests)/sd(aus_stops_final$larrests))
aus_stops_final$lmisdemeanors_sd <- aus_stops_final$lmisdemeanors - (mean(aus_stops_final$lmisdemeanors)/sd(aus_stops_final$lmisdemeanors))
aus_stops_final$lfelonies_sd <- aus_stops_final$lfelonies - (mean(aus_stops_final$lfelonies)/sd(aus_stops_final$lfelonies))
aus_stops_final$lnonviolent_sd <- aus_stops_final$lnonviolent - (mean(aus_stops_final$lnonviolent)/sd(aus_stops_final$lnonviolent))
aus_stops_final$lviolent_sd <- aus_stops_final$lviolent - (mean(aus_stops_final$lviolent)/sd(aus_stops_final$lviolent))
aus_stops_final$lsociety_sd <- aus_stops_final$lsociety - (mean(aus_stops_final$lsociety)/sd(aus_stops_final$lsociety))
aus_stops_final$lperson_sd <- aus_stops_final$lperson - (mean(aus_stops_final$lperson)/sd(aus_stops_final$lperson))
aus_stops_final$lproperty_sd <- aus_stops_final$lproperty - (mean(aus_stops_final$lproperty)/sd(aus_stops_final$lproperty))

# create scaled DVs for stop vars
aus_stops_final$lall_stops_total_sd <- aus_stops_final$lall_stops_total - (mean(aus_stops_final$lall_stops_total)/sd(aus_stops_final$lall_stops_total))
aus_stops_final$lall_stops_black_sd <- aus_stops_final$lall_stops_black - (mean(aus_stops_final$lall_stops_black)/sd(aus_stops_final$lall_stops_black))
aus_stops_final$lall_stops_latino_sd <- aus_stops_final$lall_stops_latino - (mean(aus_stops_final$lall_stops_latino)/sd(aus_stops_final$lall_stops_latino))
aus_stops_final$lall_stops_white_sd <- aus_stops_final$lall_stops_white - (mean(aus_stops_final$lall_stops_white)/sd(aus_stops_final$lall_stops_white))
aus_stops_final$lall_stops_asian_sd <- aus_stops_final$lall_stops_asian - (mean(aus_stops_final$lall_stops_asian)/sd(aus_stops_final$lall_stops_asian))
aus_stops_final$lall_stops_nonwhite_sd <- aus_stops_final$lall_stops_nonwhite - (mean(aus_stops_final$lall_stops_nonwhite)/sd(aus_stops_final$lall_stops_nonwhite))

# create binned boundary measure
aus_stops_final <- aus_stops_final %>% mutate(boundary_quart = quantile(aus_stops_final$p_race_white_blv, prob=.75, na.rm=TRUE),
                                              boundary_quart_dummy = ifelse(p_race_white_blv > boundary_quart,1,0 ))


# Milwaukee
load("mil_stops_final.RData")


# now run analyses

# log and +1 to variables (pop already logged)
mil_stops_final = mil_stops_final %>% 
  mutate(lmhhi = log(mhhi + 1),
         larrests = log(total_arrests + 1),
         lmisdemeanors = log(misdemeanor_arrests + 1),
         lfelonies = log(felony_arrests + 1),
         lnonviolent = log(nonviolent_arrests + 1),
         lviolent = log(violent_arrests + 1),
         lsociety = log(society_arrests + 1),
         lperson = log(person_arrests + 1),
         lproperty = log(property_arrests + 1),
         lcrime = log(crime_all + 1),
         lpropertycrime = log(crime_property + 1),
         lviolentcrime = log(crime_violent + 1))

# now for new stop vars
mil_stops_final = mil_stops_final %>%
  mutate(lall_stops_total = log(all_stops_total + 1),
         lall_stops_black = log(all_stops_black + 1),
         lall_stops_latino = log(all_stops_latino + 1),
         lall_stops_white = log(all_stops_white + 1),
         lall_stops_asian = log(all_stops_asian + 1),
         lall_stops_nonwhite = log(all_stops_nonwhite + 1))

# create scaled DVs (to account for different pop. sizes and density in blocks)
mil_stops_final$larrest_sd <- mil_stops_final$larrests - (mean(mil_stops_final$larrests)/sd(mil_stops_final$larrests))
mil_stops_final$lmisdemeanors_sd <- mil_stops_final$lmisdemeanors - (mean(mil_stops_final$lmisdemeanors)/sd(mil_stops_final$lmisdemeanors))
mil_stops_final$lfelonies_sd <- mil_stops_final$lfelonies - (mean(mil_stops_final$lfelonies)/sd(mil_stops_final$lfelonies))
mil_stops_final$lnonviolent_sd <- mil_stops_final$lnonviolent - (mean(mil_stops_final$lnonviolent)/sd(mil_stops_final$lnonviolent))
mil_stops_final$lviolent_sd <- mil_stops_final$lviolent - (mean(mil_stops_final$lviolent)/sd(mil_stops_final$lviolent))
mil_stops_final$lsociety_sd <- mil_stops_final$lsociety - (mean(mil_stops_final$lsociety)/sd(mil_stops_final$lsociety))
mil_stops_final$lperson_sd <- mil_stops_final$lperson - (mean(mil_stops_final$lperson)/sd(mil_stops_final$lperson))
mil_stops_final$lproperty_sd <- mil_stops_final$lproperty - (mean(mil_stops_final$lproperty)/sd(mil_stops_final$lproperty))

# create scaled DVs for stop vars
mil_stops_final$lall_stops_total_sd <- mil_stops_final$lall_stops_total - (mean(mil_stops_final$lall_stops_total)/sd(mil_stops_final$lall_stops_total))
mil_stops_final$lall_stops_black_sd <- mil_stops_final$lall_stops_black - (mean(mil_stops_final$lall_stops_black)/sd(mil_stops_final$lall_stops_black))
mil_stops_final$lall_stops_latino_sd <- mil_stops_final$lall_stops_latino - (mean(mil_stops_final$lall_stops_latino)/sd(mil_stops_final$lall_stops_latino))
mil_stops_final$lall_stops_white_sd <- mil_stops_final$lall_stops_white - (mean(mil_stops_final$lall_stops_white)/sd(mil_stops_final$lall_stops_white))
mil_stops_final$lall_stops_asian_sd <- mil_stops_final$lall_stops_asian - (mean(mil_stops_final$lall_stops_asian)/sd(mil_stops_final$lall_stops_asian))
mil_stops_final$lall_stops_nonwhite_sd <- mil_stops_final$lall_stops_nonwhite - (mean(mil_stops_final$lall_stops_nonwhite)/sd(mil_stops_final$lall_stops_nonwhite))

# create binned boundary measure
mil_stops_final <- mil_stops_final %>% mutate(boundary_quart = quantile(mil_stops_final$p_race_white_blv, prob=.75, na.rm=TRUE),
                                              boundary_quart_dummy = ifelse(p_race_white_blv > boundary_quart,1,0 ))

# Chicago
load("chi_stops_final.RData")

# now run analyses

# log and +1 to variables (pop already logged)
chi_stops_final = chi_stops_final %>% 
  mutate(lmhhi = log(mhhi + 1),
         larrests = log(total_arrests + 1),
         lmisdemeanors = log(misdemeanor_arrests + 1),
         lfelonies = log(felony_arrests + 1),
         lnonviolent = log(nonviolent_arrests + 1),
         lviolent = log(violent_arrests + 1),
         lsociety = log(society_arrests + 1),
         lperson = log(person_arrests + 1),
         lproperty = log(property_arrests + 1),
         lcrime = log(crime_all + 1),
         lpropertycrime = log(property_crime + 1),
         lviolentcrime = log(violent_crime + 1))

# now for new stop vars
chi_stops_final = chi_stops_final %>%
  mutate(lall_stops_total = log(all_stops_total + 1),
         lall_stops_black = log(all_stops_black + 1),
         lall_stops_latino = log(all_stops_latino + 1),
         lall_stops_white = log(all_stops_white + 1),
         lall_stops_asian = log(all_stops_asian + 1),
         lall_stops_nonwhite = log(all_stops_nonwhite + 1))

# create scaled DVs (to account for different pop. sizes and density in blocks)
chi_stops_final$larrest_sd <- chi_stops_final$larrests - (mean(chi_stops_final$larrests)/sd(chi_stops_final$larrests))
chi_stops_final$lmisdemeanors_sd <- chi_stops_final$lmisdemeanors - (mean(chi_stops_final$lmisdemeanors)/sd(chi_stops_final$lmisdemeanors))
chi_stops_final$lfelonies_sd <- chi_stops_final$lfelonies - (mean(chi_stops_final$lfelonies)/sd(chi_stops_final$lfelonies))
chi_stops_final$lnonviolent_sd <- chi_stops_final$lnonviolent - (mean(chi_stops_final$lnonviolent)/sd(chi_stops_final$lnonviolent))
chi_stops_final$lviolent_sd <- chi_stops_final$lviolent - (mean(chi_stops_final$lviolent)/sd(chi_stops_final$lviolent))
chi_stops_final$lsociety_sd <- chi_stops_final$lsociety - (mean(chi_stops_final$lsociety)/sd(chi_stops_final$lsociety))
chi_stops_final$lperson_sd <- chi_stops_final$lperson - (mean(chi_stops_final$lperson)/sd(chi_stops_final$lperson))
chi_stops_final$lproperty_sd <- chi_stops_final$lproperty - (mean(chi_stops_final$lproperty)/sd(chi_stops_final$lproperty))

# create scaled DVs for stop vars
chi_stops_final$lall_stops_total_sd <- chi_stops_final$lall_stops_total - (mean(chi_stops_final$lall_stops_total)/sd(chi_stops_final$lall_stops_total))
chi_stops_final$lall_stops_black_sd <- chi_stops_final$lall_stops_black - (mean(chi_stops_final$lall_stops_black)/sd(chi_stops_final$lall_stops_black))
chi_stops_final$lall_stops_latino_sd <- chi_stops_final$lall_stops_latino - (mean(chi_stops_final$lall_stops_latino)/sd(chi_stops_final$lall_stops_latino))
chi_stops_final$lall_stops_white_sd <- chi_stops_final$lall_stops_white - (mean(chi_stops_final$lall_stops_white)/sd(chi_stops_final$lall_stops_white))
chi_stops_final$lall_stops_asian_sd <- chi_stops_final$lall_stops_asian - (mean(chi_stops_final$lall_stops_asian)/sd(chi_stops_final$lall_stops_asian))
chi_stops_final$lall_stops_nonwhite_sd <- chi_stops_final$lall_stops_nonwhite - (mean(chi_stops_final$lall_stops_nonwhite)/sd(chi_stops_final$lall_stops_nonwhite))

# create binned boundary measure
chi_stops_final <- chi_stops_final %>% mutate(boundary_quart = quantile(chi_stops_final$p_race_white_blv, prob=.75, na.rm=TRUE),
                                              boundary_quart_dummy = ifelse(p_race_white_blv > boundary_quart,1,0 ))


# Austin, DV = total stops

# rename racial and economic boundary variable
aus_stops_final$aus_white_blv <- aus_stops_final$p_race_white_blv
aus_stops_final$aus_ses_blv <- aus_stops_final$ses_blv

## white boundary measure on all stops
aus.model.all.stops = lm_robust(lall_stops_total_sd ~ aus_white_blv + aus_ses_blv + lpop + p_race_white + age_15_35_male + 
                                  hhi + lmhhi + pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime, 
                                data = aus_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

summary(aus.model.all.stops)

aus.model.all.stops.sense = lm(lall_stops_total_sd ~ aus_white_blv + aus_ses_blv + lpop + p_race_white + age_15_35_male + 
                                 hhi + lmhhi + pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime, 
                               data = aus_stops_final, subset = pop > 0)


## create df of austin coefficients
coef.aus.1 <- tidy(aus.model.all.stops)

fit_cis_95 <- confint(aus.model.all.stops, level = 0.95) %>% 
  data.frame() %>%
  rename("conf.low_95" = "X2.5..",
         "conf.high_95" = "X97.5..")
fit_cis_90 <- confint(aus.model.all.stops, level = 0.90) %>% 
  data.frame() %>%
  rename("conf.low_90" = "X5..",
         "conf.high_90" = "X95..")

coef.aus.1 <- bind_cols(coef.aus.1, 
                        fit_cis_95, 
                        fit_cis_90) %>%
  rename(Variable = term,
         Coefficient = estimate,
         SE = std.error) %>%
  filter(Variable != "(Intercept)")

# extract just white boundary coef
aus_boundary_white <- coef.aus.1[1,]


# Milwaukee DV = total stops
# rename racial and economic boundary variable
mil_stops_final$mil_white_blv <- mil_stops_final$p_race_white_blv
mil_stops_final$mil_ses_blv <- mil_stops_final$ses_blv

## white boundary measure on all stops
mil.model.all.stops = lm_robust(lall_stops_total_sd ~ mil_white_blv + mil_ses_blv + lpop + p_race_white + age_15_35_male + 
                                  hhi + lmhhi + pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime, 
                                data = mil_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

summary(mil.model.all.stops)

mil.model.all.stops.sense = lm(lall_stops_total_sd ~ mil_white_blv + mil_ses_blv + lpop + p_race_white + age_15_35_male + 
                                 hhi + lmhhi + pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime, 
                               data = mil_stops_final, subset = pop > 0)


## create df of miltin coefficients
coef.mil.1 <- tidy(mil.model.all.stops)

fit_cis_95 <- confint(mil.model.all.stops, level = 0.95) %>% 
  data.frame() %>%
  rename("conf.low_95" = "X2.5..",
         "conf.high_95" = "X97.5..")
fit_cis_90 <- confint(mil.model.all.stops, level = 0.90) %>% 
  data.frame() %>%
  rename("conf.low_90" = "X5..",
         "conf.high_90" = "X95..")

coef.mil.1 <- bind_cols(coef.mil.1, 
                        fit_cis_95, 
                        fit_cis_90) %>%
  rename(Variable = term,
         Coefficient = estimate,
         SE = std.error) %>%
  filter(Variable != "(Intercept)")

# extract just white boundary coef
mil_boundary_white <- coef.mil.1[1,]


# Chicago DV = total stops
# rename racial and economic boundary variable
chi_stops_final$chi_white_blv <- chi_stops_final$p_race_white_blv
chi_stops_final$chi_ses_blv <- chi_stops_final$ses_blv

## white boundary measure on all stops
chi.model.all.stops = lm_robust(lall_stops_total_sd ~ chi_white_blv + chi_ses_blv + lpop + p_race_white + age_15_35_male + 
                                  hhi + lmhhi + pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime, 
                                data = chi_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

summary(chi.model.all.stops)

chi.model.all.stops.sense = lm(lall_stops_total_sd ~ chi_white_blv + chi_ses_blv + lpop + p_race_white + age_15_35_male + 
                                 hhi + lmhhi + pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime, 
                               data = chi_stops_final, subset = pop > 0)


## create df of chicago coefficients
coef.chi.1 <- tidy(chi.model.all.stops)

fit_cis_95 <- confint(chi.model.all.stops, level = 0.95) %>% 
  data.frame() %>%
  rename("conf.low_95" = "X2.5..",
         "conf.high_95" = "X97.5..")
fit_cis_90 <- confint(chi.model.all.stops, level = 0.90) %>% 
  data.frame() %>%
  rename("conf.low_90" = "X5..",
         "conf.high_90" = "X95..")

coef.chi.1 <- bind_cols(coef.chi.1, 
                        fit_cis_95, 
                        fit_cis_90) %>%
  rename(Variable = term,
         Coefficient = estimate,
         SE = std.error) %>%
  filter(Variable != "(Intercept)")

# extract just white boundary coef
chi_boundary_white <- coef.chi.1[1,]

## combine 3 dfs w white boundary coefs
combined.coeffs <- rbind(aus_boundary_white, chi_boundary_white, mil_boundary_white)

# combined.coeffs = combined.coeffs %>% rename("Variable" = "term", "Coefficient" = "estimate", "SE" = "std.error") %>%
#   filter(Variable != "(Intercept)")

combined.coeffs <- combined.coeffs %>% dplyr::select(-c(SE,statistic,p.value, conf.low, conf.high))

## make xaxis labels
Plot1labels <- c("Austin","Chicago","Milwaukee")

## sensitivity analysis

# robustness value exercise 

sense_out1 = 
  sensemakr(aus.model.all.stops.sense,
            treatment = "aus_white_blv",
            benchmark_covariates = "lviolentcrime",
            kd = 1)

sense_out2 = 
  sensemakr(chi.model.all.stops.sense,
            treatment = "chi_white_blv",
            benchmark_covariates = "lviolentcrime",
            kd = 1)

sense_out3 = 
  sensemakr(mil.model.all.stops.sense,
            treatment = "mil_white_blv",
            benchmark_covariates = "lviolentcrime",
            kd = 1)


rvdf = data.frame(
  
  rv_val = c(as.numeric(sense_out1$sensitivity_stats$rv_q),
             as.numeric(sense_out2$sensitivity_stats$rv_q),
             as.numeric(sense_out3$sensitivity_stats$rv_q)),
  
  variable = combined.coeffs$Variable,
  
  bound = c("9x Violent Crime", "38x Violent Crime", "591x Violent Crime")
  
)


combined.coeffs$se = (combined.coeffs$conf.high_95 - combined.coeffs$Coefficient) / 1.96
# combined.coeffs = combined.coeffs %>% filter(Variable != 'balt_white_blv')

out_meta = metagen(
  TE = combined.coeffs$Coefficient,
  seTE = combined.coeffs$se
)

# Austin: 9x violent crime
# Chicago: 38x violent crime
# Milwaukee: 591x violent crime


df_meta = data.frame(
  Variable = "Meta-analysis",
  Coefficient = out_meta$TE.random,
  df = out_meta$df.Q,
  outcome = "lall_stops_total_sd",
  se = out_meta$seTE.random,
  conf.low_95 = out_meta$TE.random - 1.96 * out_meta$seTE.random,
  conf.high_95 = out_meta$TE.random + 1.96 * out_meta$seTE.random,
  conf.low_90 = out_meta$TE.random - 1.645 * out_meta$seTE.random,
  conf.high_90 = out_meta$TE.random + 1.645 * out_meta$seTE.random
)

combined.coeffs = bind_rows(combined.coeffs, df_meta)
combined.coeffs$Variable = 
  factor(combined.coeffs$Variable, levels = combined.coeffs$Variable)


white_blv_plot_allstops <- 
  ggplot(combined.coeffs, 
         aes(x = Variable, y = Coefficient)) +
  geom_hline(yintercept = 0, 
             colour = gray(1/2), lty = 1) +
  geom_point(aes(x = Variable, 
                 y = Coefficient)) + 
  geom_errorbar(aes(x = Variable,
                    ymin = conf.low_90,
                    ymax = conf.high_90),
                width = 0,
                size = .8) +
  geom_errorbar(aes(x = Variable,
                    ymin = conf.low_95,
                    ymax = conf.high_95),
                width = 0,
                size = .4) +
  annotate("text",
           label =  paste0("RV: ", round(rvdf$rv_val[1:3], 2),
                           "\n", rvdf$bound[1:3]),
           y = combined.coeffs$Coefficient[1:3],
           x = c(1.4, 2.4, 3.45),
           size = 4,
           family = "Times") +
  geom_hline(yintercept = combined.coeffs$Coefficient[4], 
             colour = gray(1/2), lty = 2,
             alpha = .4,
             size = .2) + 
  xlab("Cities") + ylab("Racial Boundary Coefficients") + 
  scale_color_grey(start = .6, end = 0) + 
  theme_tufte(base_size = 9) + 
  scale_x_discrete(labels = c(Plot1labels, "Meta-Analysis")) + 
  theme_bw(base_size=12,base_family="Times")+
  theme(panel.border=element_rect(fill=NA, colour=NA), 
        legend.title = element_blank(),
        legend.position="bottom",
        panel.grid.minor=element_line(colour=NA)) +
  theme(strip.background = element_rect(fill="white")) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + 
  theme(
    plot.title = element_text(size = 20),
    plot.subtitle = element_text(size = 18)
  )

ggsave(width = 8, height = 6, plot = white_blv_plot_allstops, filename = "all_cities_coef_plot_stops.png")


#### reg tables associated with white boundary --> stops ####


# reg tables for boundary --> stops

aus.blv.stops = lm_robust(lall_stops_total_sd ~ p_race_white_blv + ses_blv + lpop + p_race_white + age_15_35_male + 
                            hhi + lmhhi + pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime +
                            phys_bound + com_density, 
                          data = aus_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

chi.blv.stops = lm_robust(lall_stops_total_sd ~ p_race_white_blv + ses_blv + lpop + p_race_white + age_15_35_male + 
                            hhi + lmhhi + pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime +
                            phys_bound + com_density, 
                          data = chi_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

mil.blv.stops = lm_robust(lall_stops_total_sd ~ p_race_white_blv + ses_blv + lpop + p_race_white + age_15_35_male + 
                            hhi + lmhhi + pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime +
                            phys_bound + com_density, 
                          data = mil_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

summary(aus.blv.stops)

# create latex table for white_blv --> stops
texreg(l = list(aus.blv.stops, chi.blv.stops, mil.blv.stops),
       file = "regtable.stopsmain.tex",
       include.ci = FALSE,
       custom.coef.map = list("p_race_white_blv" = "Boundary (White)",
                              "ses_blv" = "Boundary (SES)",
                              "lpop" = "Log(Population)",
                              "p_race_white" = "% White",
                              "age_15_35_male" = "Age 15-35 Male",
                              "hhi" = "Diversity",
                              "lmhhi" = "Log(MHHI)",
                              "pown" = "% Homeowner",
                              "p_poverty" = "% Poverty",
                              "p_emp_unemployed" = "% Unemployed",
                              "pcol" = "% College",
                              "lpropertycrime" = "Log(Property Crime)",
                              "lviolentcrime" = "Log(Violent Crime)",
                              "phys_bound" = "Physical Boundary",
                              "com_density" = "Commercial Density"),
       caption = "Influence of Racial Boundary on Police Stops",
       custom.header = list("Dependent Variable: Logged Police Stops" = 1:3),
       include.rmse = FALSE,
       include.adjrs = FALSE,
       caption.above = TRUE,
       float.pos = "H",
       custom.model.names = c("AUS","CHI","MIL"),
       label = "table:stops_mainfx",
       symbol = "\\dagger",
       stars = c(0.001, 0.01, 0.05, 0.1))


#### reg tables associated with boundaryXcrime --> stops ####

aus.stops.crime = lm_robust(lall_stops_total_sd ~ boundary_quart_dummy*lcrime + ses_blv + lpop + 
                              p_race_white + age_15_35_male + hhi + lmhhi + pown + p_poverty + 
                              p_emp_unemployed + pcol + phys_bound + com_density, 
                            data = aus_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

chi.stops.crime = lm_robust(lall_stops_total_sd ~ boundary_quart_dummy*lcrime + ses_blv + lpop + 
                              p_race_white + age_15_35_male + hhi + lmhhi + pown + p_poverty + 
                              p_emp_unemployed + pcol + phys_bound + com_density, 
                            data = chi_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

mil.stops.crime = lm_robust(lall_stops_total_sd ~ boundary_quart_dummy*lcrime + ses_blv + lpop + 
                              p_race_white + age_15_35_male + hhi + lmhhi + pown + p_poverty + 
                              p_emp_unemployed + pcol + phys_bound + com_density, 
                            data = mil_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

# create latex table for boundaryXcrime --> stops
texreg(l = list(aus.stops.crime, chi.stops.crime, mil.stops.crime),
       file = "regtable.crime3.tex",
       include.ci = FALSE,
       custom.coef.map = list("boundary_quart_dummy" = "Boundary (White)",
                              "lcrime" = "Log(Crime)",
                              "ses_blv" = "Boundary (SES)",
                              "lpop" = "Log(Population)",
                              "p_race_white" = "% White",
                              "age_15_35_male" = "Age 15-35 Male",
                              "hhi" = "Diversity",
                              "lmhhi" = "Log(MHHI)",
                              "pown" = "% Homeowner",
                              "p_poverty" = "% Poverty",
                              "p_emp_unemployed" = "% Unemployed",
                              "pcol" = "% College",
                              "boundary_quart_dummy:lcrime" = "Boundary X Crime",
                              "phys_bound" = "Physical Boundary",
                              "com_density" = "Commercial Density"),
       caption = "Influence of Logged Crime on Logged Stops (Standardized), Conditional on Racial Boundary Status.",
       include.rmse = FALSE,
       include.adjrs = FALSE,
       caption.above = TRUE,
       float.pos = "H",
       custom.model.names = c("AUS","CHI","MIL"),
       custom.header = list("Dependent Variable: Logged Police Stops" = 1:3),
       label = "table:stops_crimeXb",
       symbol = "\\dagger",
       stars = c(0.001, 0.01, 0.05, 0.1))


# plot interaction models for all stops - now using binned racial boundary measure
form_list = 
  list(
    'lped_stops_total_sd ~ boundary_quart_dummy*p_race_white + ses_blv + lpop + age_15_35_male + hhi + lmhhi + 
    pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime + phys_bound + com_density' )

hetdf_list = list(form_list)
hetdf_lab = c("Pedestrian Stops")

out_mod_list = as.list(rep(NA, length(form_list)))
out_df_list = as.list(rep(NA, length(form_list)))

for (i in 1:length(form_list)) {
  
  print(paste0("Iteration ", i))
  
  out_mod_list[[i]] = lm_robust(as.formula(form_list[[i]]), aus_stops_final)
  
  fakedata = 
    aus_stops_final[, names(out_mod_list[[i]]$coefficients)[2:(length(names(out_mod_list[[i]]$coefficients))-1)]] %>% 
    filter(lpop > 0) %>% 
    as.data.frame %>% 
    mutate(geometry = NULL) %>% 
    apply(X = ., MARGIN = 2, FUN = function(x) mean(x, na.rm = TRUE)) %>% 
    t %>% 
    as.data.frame %>% 
    slice(rep(1:n(), each = 22)) %>% 
    mutate(p_race_white = rep(seq(from = 0, to = 1, by = .1), 2),
           boundary_quart_dummy = c(rep(0, 11), rep(1, 11)))
  
  out_df_list[[i]] = data.frame(
    
    est = predict(object = out_mod_list[[i]], newdata = fakedata, se.fit = TRUE)$fit,
    se = predict(object = out_mod_list[[i]], newdata = fakedata, se.fit = TRUE)$se.fit,
    p_race_white = rep(seq(from = 0, to = 1, by = .1), 2),
    boundary_quart_dummy = c(rep(0, 11), rep(1, 11))
    
  ) %>% 
    mutate(dataset = hetdf_lab[i])
  
}

df_to_plot = out_df_list %>%
  do.call(rbind.data.frame, .) %>%
  mutate(boundary_quart_dummy = factor(boundary_quart_dummy, labels = c("No Boundary", "Boundary")))

df_to_plot = df_to_plot %>%
  mutate(city = "Austin")


# create plot
aus_ped_stops_race_mod_plot = df_to_plot %>%
  ggplot() +
  geom_point(aes(x = p_race_white, y = est, col = boundary_quart_dummy),
             position = position_dodge(.075),
             size = 2) +
  geom_errorbar(aes(x = p_race_white,
                    ymin = est - 1.96 * se,
                    ymax = est + 1.96 * se,
                    col = boundary_quart_dummy),
                width = 0,
                size = .4,
                position = position_dodge(.075)) +
  geom_errorbar(aes(x = p_race_white,
                    ymin = est - 1.645 * se,
                    ymax = est + 1.645 * se,
                    col = boundary_quart_dummy),
                width = 0,
                size = .6,
                position = position_dodge(.075)) +
  scale_color_grey(start = .6, end = 0) +
  labs(x = "% White",
       y = "Predicted Value",
       col = "Race Boundary") +
  facet_grid2(city ~ dataset, scales = "free_x", independent = "x") +
  theme_bw(base_size=12,base_family="Times") +
  theme(panel.border=element_rect(fill=NA, colour=NA), 
        legend.title = element_blank(),
        legend.position="none",
        panel.grid.minor=element_line(colour=NA)) +
  theme(strip.background = element_rect(fill="white")) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        axis.title.x=element_blank())

# ggsave(width = 8, height = 6, plot = aus_allstops_racial_int, filename = "austin/aus_allstops_racial_int_binned.png")


## Chicago

# plot interaction models for ped stops - now using binned racial boundary measure
form_list = 
  list(
    'lped_stops_total_sd ~ boundary_quart_dummy*p_race_white + ses_blv + lpop + age_15_35_male + hhi + lmhhi + 
    pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime + phys_bound + com_density' )

hetdf_list = list(form_list)
hetdf_lab = c("Pedestrian Stops")

out_mod_list = as.list(rep(NA, length(form_list)))
out_df_list = as.list(rep(NA, length(form_list)))

for (i in 1:length(form_list)) {
  
  print(paste0("Iteration ", i))
  
  out_mod_list[[i]] = lm_robust(as.formula(form_list[[i]]), chi_stops_final)
  
  fakedata = 
    chi_stops_final[, names(out_mod_list[[i]]$coefficients)[2:(length(names(out_mod_list[[i]]$coefficients))-1)]] %>% 
    filter(lpop > 0) %>% 
    as.data.frame %>% 
    mutate(geometry = NULL) %>% 
    apply(X = ., MARGIN = 2, FUN = function(x) mean(x, na.rm = TRUE)) %>% 
    t %>% 
    as.data.frame %>% 
    slice(rep(1:n(), each = 22)) %>% 
    mutate(p_race_white = rep(seq(from = 0, to = 1, by = .1), 2),
           boundary_quart_dummy = c(rep(0, 11), rep(1, 11)))
  
  out_df_list[[i]] = data.frame(
    
    est = predict(object = out_mod_list[[i]], newdata = fakedata, se.fit = TRUE)$fit,
    se = predict(object = out_mod_list[[i]], newdata = fakedata, se.fit = TRUE)$se.fit,
    p_race_white = rep(seq(from = 0, to = 1, by = .1), 2),
    boundary_quart_dummy = c(rep(0, 11), rep(1, 11))
    
  ) %>% 
    mutate(dataset = hetdf_lab[i])
  
}

df_to_plot = out_df_list %>%
  do.call(rbind.data.frame, .) %>%
  mutate(boundary_quart_dummy = factor(boundary_quart_dummy, labels = c("No Boundary", "Boundary")))

df_to_plot = df_to_plot %>%
  mutate(city = "Chicago")


# create plot
chi_ped_stops_race_mod_plot = df_to_plot %>%
  ggplot() +
  geom_point(aes(x = p_race_white, y = est, col = boundary_quart_dummy),
             position = position_dodge(.075),
             size = 2) +
  geom_errorbar(aes(x = p_race_white,
                    ymin = est - 1.96 * se,
                    ymax = est + 1.96 * se,
                    col = boundary_quart_dummy),
                width = 0,
                size = .4,
                position = position_dodge(.075)) +
  geom_errorbar(aes(x = p_race_white,
                    ymin = est - 1.645 * se,
                    ymax = est + 1.645 * se,
                    col = boundary_quart_dummy),
                width = 0,
                size = .6,
                position = position_dodge(.075)) +
  scale_color_grey(start = .6, end = 0) +
  labs(x = "% White",
       y = "Predicted Value",
       col = "Race Boundary") +
  facet_grid2(city ~ dataset, scales = "free_x", independent = "x") +
  theme_bw(base_size=12,base_family="Times") +
  theme(panel.border=element_rect(fill=NA, colour=NA), 
        legend.title = element_blank(),
        legend.position="none", # remove legend
        panel.grid.minor=element_line(colour=NA)) +
  theme(strip.background = element_rect(fill="white")) + # remove grey from background
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), # remove gridlines from background
        axis.title.x=element_blank(), strip.text.x = element_blank()) # remove x axis title and x labels for plots



# put all plots together
library(cowplot)
ped_stops_race_mod_plots <- plot_grid(aus_ped_stops_race_mod_plot, chi_ped_stops_race_mod_plot,
                                      align = "v", nrow = 2, rel_heights = c(1/5, 1/5))



ggsave(plot = ped_stops_race_mod_plots, filename = "ped_stops_race_mod_plots.png", 
       width = 8, height = 12, bg = 'white')



#### reg tables associated with boundary X %white --> stops ####

aus.stops.race.mod = lm_robust(lall_stops_total_sd ~ boundary_quart_dummy*p_race_white + ses_blv + lpop + age_15_35_male + hhi + lmhhi + 
                                 pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime +
                                 phys_bound + com_density, 
                               data = aus_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

chi.stops.race.mod = lm_robust(lall_stops_total_sd ~ boundary_quart_dummy*p_race_white + ses_blv + lpop + age_15_35_male + hhi + lmhhi + 
                                 pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime + 
                                 phys_bound + com_density, 
                               data = chi_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

mil.stops.race.mod = lm_robust(lall_stops_total_sd ~ boundary_quart_dummy*p_race_white + ses_blv + lpop + age_15_35_male + hhi + lmhhi + 
                                 pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime +
                                 phys_bound + com_density, 
                               data = mil_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)


# create latex table for boundary X %white --> stops
texreg(l = list(aus.stops.race.mod, chi.stops.race.mod, mil.stops.race.mod),
       file = "regtable.stops.race.mod.tex",
       include.ci = FALSE,
       custom.coef.map = list("boundary_quart_dummy" = "Boundary (White)",
                              "p_race_white" = "% White",
                              "ses_blv" = "Boundary (SES)",
                              "lpop" = "Log(Population)",
                              "age_15_35_male" = "Age 15-35 Male",
                              "hhi" = "Diversity",
                              "lmhhi" = "Log(MHHI)",
                              "pown" = "% Homeowner",
                              "p_poverty" = "% Poverty",
                              "p_emp_unemployed" = "% Unemployed",
                              "pcol" = "% College",
                              "boundary_quart_dummy:p_race_white" = "Boundary X % White",
                              "lpropertycrime" = "Log(Property Crime)",
                              "lviolentcrime" = "Log(Violent Crime)",
                              "phys_bound" = "Physical Boundary",
                              "com_density" = "Commercial Density"),
       caption = "Influence of Percent White on Logged Stops (Standardized), Conditional on Racial Boundary Status",
       include.rmse = FALSE,
       include.adjrs = FALSE,
       caption.above = TRUE,
       float.pos = "H",
       custom.model.names = c("AUS","CHI","MIL"),
       custom.header = list("Dependent Variable: Logged Police Stops" = 1:3),
       label = "table:stops_whiteXb",
       symbol = "\\dagger",
       stars = c(0.001, 0.01, 0.05, 0.1))

#### reg tables associated with boundary X %white --> stops by race ####

# Austin
aus.stops.nonwhite = lm_robust(lall_stops_nonwhite_sd ~ boundary_quart_dummy*p_race_white + ses_blv + lpop + age_15_35_male + 
                                 hhi + lmhhi + pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + 
                                 lviolentcrime + phys_bound + com_density, 
                               data = aus_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

aus.stops.white = lm_robust(lall_stops_white_sd ~ boundary_quart_dummy*p_race_white + ses_blv + lpop + age_15_35_male + 
                              hhi + lmhhi + pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + 
                              lviolentcrime + phys_bound + com_density, 
                            data = aus_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

# Chicago
chi.stops.nonwhite = lm_robust(lall_stops_nonwhite_sd ~ boundary_quart_dummy*p_race_white + ses_blv + lpop + age_15_35_male + 
                                 hhi + lmhhi + pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + 
                                 lviolentcrime + phys_bound + com_density, 
                               data = chi_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

chi.stops.white = lm_robust(lall_stops_white_sd ~ boundary_quart_dummy*p_race_white + ses_blv + lpop + age_15_35_male + 
                              hhi + lmhhi + pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + 
                              lviolentcrime + phys_bound + com_density, 
                            data = chi_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

# Milwaukee
mil.stops.nonwhite = lm_robust(lall_stops_nonwhite_sd ~ boundary_quart_dummy*p_race_white + ses_blv + lpop + age_15_35_male + 
                                 hhi + lmhhi + pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + 
                                 lviolentcrime + phys_bound + com_density, 
                               data = mil_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

mil.stops.white = lm_robust(lall_stops_white_sd ~ boundary_quart_dummy*p_race_white + ses_blv + lpop + age_15_35_male + 
                              hhi + lmhhi + pown + p_poverty + p_emp_unemployed + pcol + lpropertycrime + 
                              lviolentcrime + phys_bound + com_density, 
                            data = mil_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

# create latex table for boundary X %white --> stops by race
texreg(l = list(aus.stops.nonwhite, aus.stops.white, chi.stops.nonwhite, chi.stops.white, mil.stops.nonwhite,
                mil.stops.white),
       file = "regtable.stops.fow.tex",
       include.ci = FALSE,
       custom.coef.map = list("boundary_quart_dummy" = "Boundary (White)",
                              "p_race_white" = "% White",
                              "ses_blv" = "Boundary (SES)",
                              "lpop" = "Log(Population)",
                              "age_15_35_male" = "Age 15-35 Male",
                              "hhi" = "Diversity",
                              "lmhhi" = "Log(MHHI)",
                              "pown" = "% Homeowner",
                              "p_poverty" = "% Poverty",
                              "p_emp_unemployed" = "% Unemployed",
                              "pcol" = "% College",
                              "boundary_quart_dummy:p_race_white" = "Boundary * % White",
                              "lpropertycrime" = "Log(Property Crime)",
                              "lviolentcrime" = "Log(Violent Crime)",
                              "phys_bound" = "Physical Boundary",
                              "com_density" = "Commercial Density"),
       caption = "Influence of Percent White on Police Stops by Race of Civilian, Conditional on Racial Boundary Status",
       include.rmse = FALSE,
       include.adjrs = FALSE,
       caption.above = TRUE,
       float.pos = "H",
       custom.model.names = c("Non-white","White","Non-white","White",
                              "Non-white","White"),
       custom.header = list("Dependent Variable: Logged Police Stops" = 1:6),
       label = "table:stops_FOW",
       symbol = "\\dagger",
       stars = c(0.001, 0.01, 0.05, 0.1))


###### boundary_quart_dummyXp_poverty plots w race of person stopped #####
## Austin

form_list = 
  list(
    'lall_stops_nonwhite_sd ~ boundary_quart_dummy*p_poverty+ p_race_white + ses_blv + lpop + age_15_35_male + hhi + lmhhi + 
    pown + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime + com_density + phys_bound',
    'lall_stops_white_sd ~ boundary_quart_dummy*p_poverty + p_race_white + ses_blv + lpop + age_15_35_male + hhi + lmhhi + 
    pown + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime + com_density + phys_bound'
  )

hetdf_list = list(form_list)
hetdf_lab = c("Non-white Persons","White Persons")

out_mod_list = as.list(rep(NA, length(form_list)))
out_df_list = as.list(rep(NA, length(form_list)))

for (i in 1:length(form_list)) {
  
  print(paste0("Iteration ", i))
  
  out_mod_list[[i]] = lm_robust(as.formula(form_list[[i]]), aus_stops_final)
  
  fakedata = 
    aus_stops_final[, names(out_mod_list[[i]]$coefficients)[2:(length(names(out_mod_list[[i]]$coefficients))-1)]] %>% 
    filter(lpop > 0) %>% 
    as.data.frame %>% 
    mutate(geometry = NULL) %>% 
    apply(X = ., MARGIN = 2, FUN = function(x) mean(x, na.rm = TRUE)) %>% 
    t %>% 
    as.data.frame %>% 
    slice(rep(1:n(), each = 22)) %>% 
    mutate(p_poverty = rep(seq(from = 0, to = 1, by = .1), 2),
           boundary_quart_dummy = c(rep(0, 11), rep(1, 11)))
  
  out_df_list[[i]] = data.frame(
    
    est = predict(object = out_mod_list[[i]], newdata = fakedata, se.fit = TRUE)$fit,
    se = predict(object = out_mod_list[[i]], newdata = fakedata, se.fit = TRUE)$se.fit,
    p_poverty = rep(seq(from = 0, to = 1, by = .1), 2),
    boundary_quart_dummy = c(rep(0, 11), rep(1, 11))
    
  ) %>% 
    mutate(dataset = hetdf_lab[i])
  
}

df_to_plot = out_df_list %>%
  do.call(rbind.data.frame, .) %>%
  mutate(boundary_quart_dummy = factor(boundary_quart_dummy, labels = c("No Boundary", "Boundary")))

df_to_plot = df_to_plot %>%
  mutate(city = "Austin")

# create plot
aus_stops_pov_fow_plot = df_to_plot %>%
  ggplot() +
  geom_point(aes(x = p_poverty, y = est, col = boundary_quart_dummy),
             position = position_dodge(.075),
             size = 2) +
  geom_errorbar(aes(x = p_poverty,
                    ymin = est - 1.96 * se,
                    ymax = est + 1.96 * se,
                    col = boundary_quart_dummy),
                width = 0,
                size = .4,
                position = position_dodge(.075)) +
  geom_errorbar(aes(x = p_poverty,
                    ymin = est - 1.645 * se,
                    ymax = est + 1.645 * se,
                    col = boundary_quart_dummy),
                width = 0,
                size = .6,
                position = position_dodge(.075)) +
  scale_color_grey(start = .6, end = 0) +
  labs(x = "% Poverty",
       y = "Predicted Value",
       col = "Race Boundary") +
  facet_grid2(city ~ dataset, scales = "free_x", independent = "x") +
  theme_bw(base_size=12,base_family="Times") +
  theme(panel.border=element_rect(fill=NA, colour=NA), 
        legend.title = element_blank(),
        legend.position="none",
        panel.grid.minor=element_line(colour=NA)) +
  theme(strip.background = element_rect(fill="white")) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        axis.title.x=element_blank())


## Chicago
form_list = 
  list(
    'lall_stops_nonwhite_sd ~ boundary_quart_dummy*p_poverty + p_race_white + ses_blv + lpop + age_15_35_male + hhi + lmhhi + 
    pown + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime + com_density + phys_bound',
    'lall_stops_white_sd ~ boundary_quart_dummy*p_poverty + p_race_white + ses_blv + lpop + age_15_35_male + hhi + lmhhi + 
    pown + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime + com_density + phys_bound'
  )

hetdf_list = list(form_list)
hetdf_lab = c("Non-white Persons","White Persons")

out_mod_list = as.list(rep(NA, length(form_list)))
out_df_list = as.list(rep(NA, length(form_list)))

for (i in 1:length(form_list)) {
  
  print(paste0("Iteration ", i))
  
  out_mod_list[[i]] = lm_robust(as.formula(form_list[[i]]), chi_stops_final)
  
  fakedata = 
    chi_stops_final[, names(out_mod_list[[i]]$coefficients)[2:(length(names(out_mod_list[[i]]$coefficients))-1)]] %>% 
    filter(lpop > 0) %>% 
    as.data.frame %>% 
    mutate(geometry = NULL) %>% 
    apply(X = ., MARGIN = 2, FUN = function(x) mean(x, na.rm = TRUE)) %>% 
    t %>% 
    as.data.frame %>% 
    slice(rep(1:n(), each = 22)) %>% 
    mutate(p_poverty = rep(seq(from = 0, to = 1, by = .1), 2),
           boundary_quart_dummy = c(rep(0, 11), rep(1, 11)))
  
  out_df_list[[i]] = data.frame(
    
    est = predict(object = out_mod_list[[i]], newdata = fakedata, se.fit = TRUE)$fit,
    se = predict(object = out_mod_list[[i]], newdata = fakedata, se.fit = TRUE)$se.fit,
    p_poverty = rep(seq(from = 0, to = 1, by = .1), 2),
    boundary_quart_dummy = c(rep(0, 11), rep(1, 11))
    
  ) %>% 
    mutate(dataset = hetdf_lab[i])
  
}

df_to_plot = out_df_list %>%
  do.call(rbind.data.frame, .) %>%
  mutate(boundary_quart_dummy = factor(boundary_quart_dummy, labels = c("No Boundary", "Boundary")))

df_to_plot = df_to_plot %>%
  mutate(city = "Chicago")

# create plot
library(ggh4x)
chi_stops_pov_fow_plot = df_to_plot %>%
  ggplot() +
  geom_point(aes(x = p_poverty, y = est, col = boundary_quart_dummy),
             position = position_dodge(.075),
             size = 2) +
  geom_errorbar(aes(x = p_poverty,
                    ymin = est - 1.96 * se,
                    ymax = est + 1.96 * se,
                    col = boundary_quart_dummy),
                width = 0,
                size = .4,
                position = position_dodge(.075)) +
  geom_errorbar(aes(x = p_poverty,
                    ymin = est - 1.645 * se,
                    ymax = est + 1.645 * se,
                    col = boundary_quart_dummy),
                width = 0,
                size = .6,
                position = position_dodge(.075)) +
  scale_color_grey(start = .6, end = 0) +
  labs(x = "% Poverty",
       y = "Predicted Value",
       col = "Race Boundary") +
  facet_grid2(city ~ dataset, scales = "free_x", independent = "x") +
  theme_bw(base_size=12,base_family="Times") +
  theme(panel.border=element_rect(fill=NA, colour=NA), 
        legend.title = element_blank(),
        legend.position="none", # remove legend
        panel.grid.minor=element_line(colour=NA)) +
  theme(strip.background = element_rect(fill="white")) + # remove grey from background
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), # remove gridlines from background
        axis.title.x=element_blank(), strip.text.x = element_blank()) # remove x axis title and x labels for plots




## Milwaukee
form_list = 
  list(
    'lall_stops_nonwhite_sd ~ boundary_quart_dummy*p_poverty + p_race_white + ses_blv + lpop + age_15_35_male + hhi + lmhhi + 
    pown + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime + com_density + phys_bound',
    'lall_stops_white_sd ~ boundary_quart_dummy*p_poverty + p_race_white + ses_blv + lpop + age_15_35_male + hhi + lmhhi + 
    pown + p_emp_unemployed + pcol + lpropertycrime + lviolentcrime + com_density + phys_bound'
  )

hetdf_list = list(form_list)
hetdf_lab = c("Non-white Persons","White Persons")

out_mod_list = as.list(rep(NA, length(form_list)))
out_df_list = as.list(rep(NA, length(form_list)))

for (i in 1:length(form_list)) {
  
  print(paste0("Iteration ", i))
  
  out_mod_list[[i]] = lm_robust(as.formula(form_list[[i]]), mil_stops_final)
  
  fakedata = 
    mil_stops_final[, names(out_mod_list[[i]]$coefficients)[2:(length(names(out_mod_list[[i]]$coefficients))-1)]] %>% 
    filter(lpop > 0) %>% 
    as.data.frame %>% 
    mutate(geometry = NULL) %>% 
    apply(X = ., MARGIN = 2, FUN = function(x) mean(x, na.rm = TRUE)) %>% 
    t %>% 
    as.data.frame %>% 
    slice(rep(1:n(), each = 22)) %>% 
    mutate(p_poverty = rep(seq(from = 0, to = 1, by = .1), 2),
           boundary_quart_dummy = c(rep(0, 11), rep(1, 11)))
  
  out_df_list[[i]] = data.frame(
    
    est = predict(object = out_mod_list[[i]], newdata = fakedata, se.fit = TRUE)$fit,
    se = predict(object = out_mod_list[[i]], newdata = fakedata, se.fit = TRUE)$se.fit,
    p_poverty = rep(seq(from = 0, to = 1, by = .1), 2),
    boundary_quart_dummy = c(rep(0, 11), rep(1, 11))
    
  ) %>% 
    mutate(dataset = hetdf_lab[i])
  
}

df_to_plot = out_df_list %>%
  do.call(rbind.data.frame, .) %>%
  mutate(boundary_quart_dummy = factor(boundary_quart_dummy, labels = c("No Boundary", "Boundary")))

df_to_plot = df_to_plot %>%
  mutate(city = "Milwaukee")

# create plot
mil_stops_pov_fow_plot = df_to_plot %>%
  ggplot() +
  geom_point(aes(x = p_poverty, y = est, col = boundary_quart_dummy),
             position = position_dodge(.075),
             size = 2) +
  geom_errorbar(aes(x = p_poverty,
                    ymin = est - 1.96 * se,
                    ymax = est + 1.96 * se,
                    col = boundary_quart_dummy),
                width = 0,
                size = .4,
                position = position_dodge(.075)) +
  geom_errorbar(aes(x = p_poverty,
                    ymin = est - 1.645 * se,
                    ymax = est + 1.645 * se,
                    col = boundary_quart_dummy),
                width = 0,
                size = .6,
                position = position_dodge(.075)) +
  scale_color_grey(start = .6, end = 0) +
  labs(x = "% Poverty",
       y = "Predicted Value",
       col = "Race Boundary") +
  facet_grid2(city ~ dataset, scales = "free_x", independent = "x") +
  theme_bw(base_size=12,base_family="Times") +
  theme(panel.border=element_rect(fill=NA, colour=NA), 
        legend.title = element_blank(),
        legend.position="bottom",
        panel.grid.minor=element_line(colour=NA)) +
  theme(strip.background = element_rect(fill="white")) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        strip.text.x = element_blank())


# put all the plots together
library(cowplot)
pov_fow_plots <- plot_grid(aus_stops_pov_fow_plot, chi_stops_pov_fow_plot, mil_stops_pov_fow_plot,
                           align = "v", nrow = 3, rel_heights = c(1/3, 1/3, 1/2.5))

ggsave(plot = pov_fow_plots, filename = "pov_fow_plots.png", 
       width = 8, height = 12, bg = 'white')




#### reg tables associated with boundary X p_poverty --> stops by race ####

# Austin
aus.stops.nonwhite = lm_robust(lall_stops_nonwhite_sd ~ boundary_quart_dummy*p_poverty + ses_blv + lpop + age_15_35_male + 
                                 hhi + lmhhi + pown + p_race_white + p_emp_unemployed + pcol + lpropertycrime + 
                                 lviolentcrime + phys_bound + com_density, 
                               data = aus_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

aus.stops.white = lm_robust(lall_stops_white_sd ~ boundary_quart_dummy*p_poverty + ses_blv + lpop + age_15_35_male + 
                              hhi + lmhhi + pown + p_race_white + p_emp_unemployed + pcol + lpropertycrime + 
                              lviolentcrime + phys_bound + com_density, 
                            data = aus_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

# Chicago
chi.stops.nonwhite = lm_robust(lall_stops_nonwhite_sd ~ boundary_quart_dummy*p_poverty + ses_blv + lpop + age_15_35_male + 
                                 hhi + lmhhi + pown + p_race_white + p_emp_unemployed + pcol + lpropertycrime + 
                                 lviolentcrime + phys_bound + com_density, 
                               data = chi_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

chi.stops.white = lm_robust(lall_stops_white_sd ~ boundary_quart_dummy*p_poverty + ses_blv + lpop + age_15_35_male + 
                              hhi + lmhhi + pown + p_race_white + p_emp_unemployed + pcol + lpropertycrime + 
                              lviolentcrime + phys_bound + com_density, 
                            data = chi_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

# Milwaukee
mil.stops.nonwhite = lm_robust(lall_stops_nonwhite_sd ~ boundary_quart_dummy*p_poverty + ses_blv + lpop + age_15_35_male + 
                                 hhi + lmhhi + pown + p_race_white + p_emp_unemployed + pcol + lpropertycrime + 
                                 lviolentcrime + phys_bound + com_density, 
                               data = mil_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

mil.stops.white = lm_robust(lall_stops_white_sd ~ boundary_quart_dummy*p_poverty + ses_blv + lpop + age_15_35_male + 
                              hhi + lmhhi + pown + p_race_white + p_emp_unemployed + pcol + lpropertycrime + 
                              lviolentcrime + phys_bound + com_density, 
                            data = mil_stops_final, cluster = BG_CODE, se_type = "stata", subset = pop > 0)

# create latex table for boundary X %white --> stops by race
texreg(l = list(aus.stops.nonwhite, aus.stops.white, chi.stops.nonwhite, chi.stops.white, mil.stops.nonwhite,
                mil.stops.white),
       file = "regtable.raceXclass.stops.fow.tex",
       include.ci = FALSE,
       custom.coef.map = list("boundary_quart_dummy" = "Boundary (White)",
                              "p_race_white" = "% White",
                              "ses_blv" = "Boundary (SES)",
                              "lpop" = "Log(Population)",
                              "age_15_35_male" = "Age 15-35 Male",
                              "hhi" = "Diversity",
                              "lmhhi" = "Log(MHHI)",
                              "pown" = "% Homeowner",
                              "p_poverty" = "% Poverty",
                              "p_emp_unemployed" = "% Unemployed",
                              "pcol" = "% College",
                              "boundary_quart_dummy:p_poverty" = "Boundary * % Poverty",
                              "lpropertycrime" = "Log(Property Crime)",
                              "lviolentcrime" = "Log(Violent Crime)"),
       caption = "Influence of Percent Poverty on Police Stops by Race of Civilian, Conditional on Racial Boundary Status",
       include.rmse = FALSE,
       include.adjrs = FALSE,
       caption.above = TRUE,
       float.pos = "H",
       custom.model.names = c("Non-white","White","Non-white","White",
                              "Non-white","White"),
       custom.header = list("Dependent Variable: Logged Police Stops" = 1:6),
       label = "table:raceXclass_stops_FOW",
       symbol = "\\dagger",
       stars = c(0.001, 0.01, 0.05, 0.1))


